package cn.wolfcode.crm.util;


import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemLog;
import cn.wolfcode.crm.mapper.SystemLogMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class SystemLogUtil {
    @Autowired
    private SystemLogMapper systemLogMapper;


    public void write(JoinPoint joinPoint){
        SystemLog log = new SystemLog();
        //操作人id
        Employee user = UserContext.getCurrentUser();
        log.setOpuserId(user.getId());
        log.setOpuserName(user.getUsername());
        //通过session可以获取ip
        log.setOpip(UserContext.getSession().getHost());
        log.setOptime(new Date());
        //类名
        String claSSName = joinPoint.getTarget().getClass().getName();
        //方法名
        String methodName = joinPoint.getSignature().getName();
        log.setFunction(claSSName+":"+methodName);
        Object[] args = joinPoint.getArgs();
        //那对象转成字符串
        try {
            String param = new ObjectMapper().writeValueAsString(args);
        log.setParams(param);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        systemLogMapper.insert(log);
    }
}
